package com.bee.bootstrap.resource.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bee.bootstrap.resource.entity.Resource;
import com.bee.bootstrap.resource.mapper.ResourceMapper;
import com.bee.bootstrap.resource.service.IResourceService;
import com.bee.common.core.enums.ResourceTypeEnum;
import com.bee.common.core.enums.StatusEnum;
import com.bee.common.core.result.Result;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liekkas
 * @since 2018-05-25
 */
@Service("resourceServiceImpl")
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> implements IResourceService{

    @Override
    public Result findPageView(Page<Resource> page, Resource resource) {
        page=this.selectPage(page,new EntityWrapper<>(resource));
        return Result.success(page);
    }

    @Override
    public Result selectTreeView() {
      List<Resource> all=this.selectList(new EntityWrapper<Resource>().eq("status", StatusEnum.NORMAL.toString()));
      List<Resource> parents=this.selectList(new EntityWrapper<Resource>().isNull("parent_id").eq("status", StatusEnum.NORMAL.toString()).orderBy("priority",true));
      parents.forEach(parent->{
          parent.setChildrensView(getChild(parent.getId(),all));
      });

      return Result.success(parents);
    }

    /**
     * 递归查找子资源
     * @param id
     * @param all
     * @return
     */
    private List<Resource> getChild(Long id, List<Resource> all) {
        List<Resource> childrens=new ArrayList<>();
        all.forEach(resource -> {
            if(resource.getParentId()!=null&&resource.getParentId().compareTo(id)==0){
                childrens.add(resource);
            }
        });
        //子资源的再遍历一次
        childrens.forEach(children->{
            children.setChildrensView(getChild(children.getId(),all));
        });
        Collections.sort(childrens, new Comparator<Resource>() {
            @Override
            public int compare(Resource o1, Resource o2) {
                if(o1.getPriority()!=null&&o2.getPriority()!=null)
                    return o1.getPriority().compareTo(o2.getPriority());
                else
                    return 0;
            }
        });//排序
        if(childrens.size()==0){
            return new ArrayList<Resource>();
        }
        return childrens;
    }



    @Override
    public Result updateResource(Resource resource) {
        Resource persistence=this.selectById(resource.getId());
        //更新fullparentids
        if(resource.getParentId()==null&&persistence.getParentId()!=null){
            Resource condition=new Resource();
            condition.setParentId(resource.getId());
            condition.setStatus(StatusEnum.NORMAL.toString());
            List<Resource> childrens=this.selectList(new EntityWrapper<Resource>(condition));
            if(childrens!=null&&childrens.size()>0){
                childrens.forEach(children->{
                    children.setUpdateTime(new Date());
                    children.setFullParentIds(children.getFullParentIds().replace(persistence.getParentId()+"/",""));
                });
                this.updateBatchById(childrens);
            }
        }else{
            Resource condition=new Resource();
            condition.setParentId(resource.getId());
            condition.setStatus(StatusEnum.NORMAL.toString());
            List<Resource> childrens=this.selectList(new EntityWrapper<Resource>(condition));
            if(childrens!=null&&childrens.size()>0){
                childrens.forEach(children->{
                    children.setUpdateTime(new Date());
                    children.setFullParentIds(resource.getParentId()+"/"+children.getFullParentIds());
                });
                this.updateBatchById(childrens);
            }
        }
        resource.setUpdateTime(new Date());
        this.updateAllColumnById(resource);
        return Result.success("修改成功");
    }

    @Override
    public Result insertResource(Resource resource) {
        this.insert(resource);
        return Result.success("保存成功");
    }

    @Override
    public Result findResourcesInterface() {
        return Result.success(this.selectList(new EntityWrapper<Resource>().
                eq("type",ResourceTypeEnum.INTERFACE.toString()).
                eq("status",StatusEnum.NORMAL.toString()).like("permission","read")));
    }
}
